package com.bnlr.lrparking.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.bnlr.lrparking.R;

/**
 * textView样式
 * @author tian
 */

public class SuperTextView extends AppCompatTextView {
    private Context context;
    /**
     * 填充颜色
     */
    private int fillColor;
    /**
     * 边框圆半径
     */
    private int borderRadius;
    /**
     * 边框宽度
     */
    private int borderWidth;
    /**
     * 边框颜色
     */
    private int borderColor;
    /**
     * 左内间距
     */
    private int leftPadding;
    /**
     * 上内间距
     */
    private int topPadding;
    /**
     * 右内间距
     */
    private int rightPadding;
    /**
     * 下内间距
     */
    private int bottomPadding;
    /**
     * 按压颜色
     */
    private int pressedColor;
    /**
     * 能否点击
     */
    private boolean clickEnable;
    /**
     * 要设置的背景drawable
     */
    private GradientDrawable gradientDrawable;
    /**
     * 点击监听
     */
    private OnTextViewClickListener onTextViewClickListener;
    public SuperTextView(Context context) {
        this(context,null);
    }
    public SuperTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        TypedArray t = context.obtainStyledAttributes(attrs, R.styleable.SuperTextView);
        fillColor = t.getColor(R.styleable.SuperTextView_fill_color, Color.WHITE);
        borderRadius = t.getInteger(R.styleable.SuperTextView_border_radius,3);
        borderWidth = t.getInteger(R.styleable.SuperTextView_border_width,0);
        borderColor = t.getColor(R.styleable.SuperTextView_border_color,Color.WHITE);
        leftPadding = t.getInteger(R.styleable.SuperTextView_left_padding,8);
        topPadding = t.getInteger(R.styleable.SuperTextView_top_padding,3);
        rightPadding = t.getInteger(R.styleable.SuperTextView_right_padding,8);
        bottomPadding = t.getInteger(R.styleable.SuperTextView_bottom_padding,3);
        pressedColor = t.getColor(R.styleable.SuperTextView_pressed_color,fillColor);
        clickEnable = t.getBoolean(R.styleable.SuperTextView_text_click,true);
        t.recycle();
        init();
    }

    public SuperTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 点击监听初始化
     * @param onTextViewClickListener
     */
    public void setOnTextViewClickListener(OnTextViewClickListener onTextViewClickListener) {
        this.onTextViewClickListener = onTextViewClickListener;
    }

    /**
     * 初始化
     */
    private void init() {
        //创建drawable
        gradientDrawable = new GradientDrawable();
        gradientDrawable.setColor(fillColor);
        gradientDrawable.setCornerRadius(dp2px(borderRadius));
        gradientDrawable.setStroke(borderWidth, borderColor);
        setPadding(leftPadding,topPadding,rightPadding,bottomPadding);
        setBackgroundDrawable(gradientDrawable);
        /*int colors[] = { 0xff255779 , 0xff3e7492, 0xffa6c0cd };//分别为开始颜色，中间夜色，结束颜色
        GradientDrawable gd = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, colors);*/
    }

    /**
     * 设置控件能否点击
     * @param enable
     */
    public void setEnables (boolean enable) {
        clickEnable = enable;
    }
    /**
     * 设置背景颜色
     * @param color
     */
    public void setBackGround (int color) {
        this.fillColor = color;
        gradientDrawable.setColor(color);
        setBackgroundDrawable(gradientDrawable);
    }
    /**
     * 设置按压后的颜色
     * @param color
     */
    public void setPressedColor (int color) {
        pressedColor = color;
    }
    /**
     * 拦截点击 配置点击事件 按压颜色
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            //  按下
            case MotionEvent.ACTION_DOWN:
                if (!clickEnable) {
                    return false;
                }
                if (gradientDrawable != null && clickEnable) {
                    gradientDrawable.setColor(pressedColor);
                }
                return true;
            //  抬起
            case MotionEvent.ACTION_UP:
                if (!clickEnable) {
                    return false;
                }
                if (gradientDrawable != null) {
                    gradientDrawable.setColor(fillColor);
                }
                if (onTextViewClickListener != null ) {
                    onTextViewClickListener.onClick(this);
                }
                return true;
            //  取消
            case MotionEvent.ACTION_CANCEL:
                if (!clickEnable) {
                    return false;
                }
                if (gradientDrawable != null ) {
                    gradientDrawable.setColor(fillColor);
                }
                return true;

            default:
                break;
        }
        return super.onTouchEvent(event);
    }


    public interface OnTextViewClickListener {
        /**
         * 控件点击
         * @param v
         */
        void onClick(View v);
    }

    /**
     * dp转px
     * @param dpValue dp值
     * @return px值
     */
    public  int dp2px(final float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * px转dp
     *
     * @param pxValue px值
     * @return dp值
     */
    public  int px2dp(final float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
